package it.yuzuojian.com.service.impl;


import it.yuzuojian.com.service.GeneratorService;
import it.yuzuojian.com.util.DataSourceUtil;
import it.yuzuojian.com.util.GenUtils;
import org.apache.commons.io.IOUtils;


import org.springframework.jdbc.core.JdbcTemplate;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.zip.ZipOutputStream;

public class GeneratorServiceImpl implements GeneratorService {

    DataSourceUtil dataSourceUtil = new DataSourceUtil();

    @Override
    public String[] getAllTable(String database) throws IOException {
        JdbcTemplate jdbcTemplate = dataSourceUtil.getJdbcTemplate();
        String sql = "SELECT A.TABLE_NAME as TableName FROM INFORMATION_SCHEMA.TABLES A WHERE  " +
                "  A.TABLE_SCHEMA = ? ";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, database);
        List<String> temp = new ArrayList<>();
        String[] strings = new String[list.size()];
        for (Map<String, Object> stringObjectMap : list) {
            temp.add(stringObjectMap.get("TABLENAME")==null ? " " : stringObjectMap.get("TABLENAME").toString());
        }
        return temp.toArray(strings);
    }

    @Override
    public List<Map<String, String>> getListColumnsByTableName(String tableName) throws IOException {
        JdbcTemplate jdbcTemplate = dataSourceUtil.getJdbcTemplate();
        String sql = "select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns " +
                "   where table_name =? and table_schema = (select database()) order by ordinal_position";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, tableName);
        List<Map<String, String>> result = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : list) {
            Map<String, String > map = new HashMap<>();
            stringObjectMap.forEach((key,value)->{
                map.put(key,value==null ? " " : value.toString());
            });
            result.add(map);
        }
        return result;
    }

    @Override
    public Map<String, String> getTableInfoByTableName(String tableName) throws IOException {
        JdbcTemplate jdbcTemplate = dataSourceUtil.getJdbcTemplate();
        String sql = "select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables " +
                "  where table_schema = (select database()) and table_name = ? ";
        Map<String, Object> map = jdbcTemplate.queryForMap(sql, tableName);
        Map<String, String> result = new HashMap<>();
        map.forEach((key,value)->{
            result.put(key,value==null ? " " : value.toString());
        });

        return result;
    }

    @Override
    public byte[] generatorCodeWeb(String[] tableNames) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        GenUtils genUtils = new GenUtils();
        for(String tableName : tableNames){
            //查询表信息
            Map<String, String> table = getTableInfoByTableName(tableName);
            //查询列信息
            List<Map<String, String>> columns = getListColumnsByTableName(tableName);
            //生成代码
            try {
                genUtils.generatorCode(table, columns, zip);
            } catch (Exception e ) {
                e.printStackTrace();
            }
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    public void generatorCode(String[] tableNames) throws IOException {
        GenUtils genUtils = new GenUtils();
        for(String tableName : tableNames){
            //查询表信息
            Map<String, String> table = getTableInfoByTableName(tableName);
            //查询列信息
            List<Map<String, String>> columns = getListColumnsByTableName(tableName);
            //生成代码
            try {
                genUtils.generatorCode(table, columns);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
